<?php


namespace app\logic\recharge;


use app\utils\Helper;
use coco\Exception;
use coco\Log;
use coco\Request;
use coco\Response;

class Huyu extends Base implements Template
{

    public static function info(): array
    {
        return [
            'support_type' => 1,//支持类型
            'name' => '互娱',
            'cfg' => [
                //name 名称 required是否必填(默认必填) allow_edit种植期间是否允许修改(默认不允许)
                'code' => ['name' => '商户号'],
                'secret' => ['name' => '签名密钥', 'required' => false],
                'notify_secret' => ['name' => '通知密钥', 'required' => false, 'allow_edit' => true],
                'admin_url' => ['name' => '后台地址', 'allow_edit' => true],
                'api_url' => ['name' => '网关', 'allow_edit' => true],
                'ip_white_lists' => ['name' => '回调白名单', 'placeholder' => '多个逗号分割,不填表示不限制', 'allow_edit' => true, 'required' => false],
                'is_test' => ['name' => '是否提交测试订单', 'type' => 'radio', 'radio' => [
                    ['name' => '是', 'value' => 1,],
                    ['name' => '否', 'value' => 0,]
                ]],
            ],
        ];
    }

    /**
     * @param array $param
     * @return array|false
     * 返回false表示明确失败  订单会处理为失败状态
     * 其他表示成功或者未知 未知情况需要手动确认 或者定时查询
     * 时间间隔不一定 自行处理
     */
    public function submit(array $param)
    {
        return ['is_confirm' => 1, 'log' => 'test'];
        $params = [
            "mch_id" => $this->cfg['code'],
            "out_trade_no" => $param['trade_no'],
            "phone" => $param['order']['uuid'],
            "price" => $param['price'],
            'is_test' => $this->cfg['is_test'],
            'notify_url' => $this->notify_url
        ];

        $headers = array(
            "Content-Type:application/x-www-form-urlencoded;charset=utf-8",
        );
        $data = http_build_query($params);
        $url = $this->cfg['api_url'] . '/api/mch/hd';
        $log = [];
        $log['url'] = $url;
        $log['data'] = $data;
        if (false === $result = Helper::http_post($url, $data, $headers, 30)) {
            $log['response'] = 'curl fail';
            return ['is_confirm' => 1, 'log' => $log];
        }
        $log['response'] = $result;
        $ret = json_decode($result, true);
        if (empty($ret)) {
            $log['decode'] = '服务器响应错误';
            return ['is_confirm' => 1, 'log' => $log];
        }
        if ($ret['status'] !== 1) {
            $this->error = $ret['msg'] ?: '未知错误' . $result;
            return false;
        }
        return ['log' => $log];
    }

    public function notify(Request $request, string $ip)
    {
        if (!empty($this->cfg['ip_white_lists'])) {
            $ip_white_lists = explode(',', $this->cfg['ip_white_lists']);
            if (!in_array($ip, $ip_white_lists)) {
                $this->error = '非法请求';
                return false;
            }
        }
        if (!empty($this->cfg['notify_secret'])) {
            $param = $request->post();
            if (empty($param)) {
                $this->error = '缺少签名';
                return false;
            }
            $sign = $param['sign'];
            unset($param['sign']);
            if ($sign !== Helper::getSign($param, $this->cfg['notify_secret'])) {
                $this->error = '签名错误';
                return false;
            }
        }
        $status = $request->post('status/d');
        $success = [1,];
        $fail = [-1,];
        $ignore = [];
        if (in_array($status, $success)) {
            $voucher = $request->post('voucher/s');
            $ret = 'success';
        } elseif (in_array($status, $fail)) {
            $msg = '渠道充值失败';
            $ret = 'fail';
        } elseif (in_array($status, $ignore)) {
            $ret = 'ignore';
        } else {
            throw new Exception('错误的状态');
        }
        $out_trade_no = $request->post('out_trade_no/s');
        return ['ret' => $ret, 'msg' => $msg ?? '', 'trade_no' => $out_trade_no, 'voucher' => $voucher];
    }

    public function notifyResponse(): Response
    {
        $response = new Response(['type' => 'txt']);
        $response->setData(['txt' => 'success']);
        return $response;
    }

    public function query(array $param, int $act = 0)
    {
        // TODO: Implement query() method.
    }
}